package com.burgeon.backend.Rest.User;



import com.burgeon.backend.Rest.base.RESTBase;
import com.burgeon.backend.entity.User;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.text.SimpleDateFormat;
import java.util.Base64;
import java.util.Date;
import java.util.HashMap;

public class CreateUser extends RESTBase {

    private String ip;
    private String account;
    private String password;

    public CreateUser(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     */

    public void getData(User user) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";

        String str = RESULT(Data(user), url);
        System.err.println("str="+str);
        String code = new JSONArray(str).getJSONObject(0).getString("code");

        if (!"0".equals(code)) {
            throw new Exception(str);
        }

    }

    private MultiValueMap Data(User user) {

        HashMap params = new HashMap();
        params.put("'C_VIPTYPE_ID__NAME'",user.getType());
        params.put("'C_CUSTOMER_ID__NAME'",user.getCustomer());
        params.put("'C_STORE_ID__NAME'",user.getStore());
        params.put("C_STOREJH_ID__NAME",user.getStorejh());


        params.put("MOBIL",user.getPhone().replaceAll("[a-zA-Z]","" ));
        params.put("SEX",user.getGender());
        params.put("PHONE",user.getPhone().replaceAll("[a-zA-Z]","" ));
        if (null != user.getOpenid()&&!"".equals(user.getOpenid())){
            params.put("OPENID","'"+user.getOpenid()+"'");
        }
        if (null!=user.getEmp()){
            params.put("HR_EMPLOYEE_ID__NAME","'"+user.getEmp()+"'");
            params.put("HR_EMPLOYEE_ID1__NAME","'"+user.getEmp()+"'");
        }
        if (null != user.getNickname()){
            params.put("WXCODE","'"+strConvertBase(user.getNickname())+"'");
        }
        params.put("CARDNO",user.getPhone());
        params.put("PASS_WORD","e717a8e574");//系统错误
        params.put("VIPSTATE","Y");//是否有效
        params.put("ISACTIVE","Y");

        if (null != user.getAppOpenid()){
            params.put("OPENID2",user.getAppOpenid());
        }


        Date now = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String dateString = sdf.format(now);

        params.put("OPENCARDDATE",dateString);
        params.put("VIPNAME",user.getName());
        params.put("BIRTHDAY",user.getBirthday());

        params.put("table", "C_VIP");//表名

        return DATA("ObjectCreate", params, account, password);
    }
    //转码
    public static String strConvertBase(String str) {
        if(null != str){
            Base64.Encoder encoder = Base64.getEncoder();
            return encoder.encodeToString(str.getBytes());
        }
        return null;
    }

    public static void main(String[] args) {
        CreateUser g = new CreateUser("59.110.28.94:90", "nea@burgeon.com.cn", "burgeon55321");
        try {


            User user = new User();
            user.setType("普通卡");
            user.setCustomer("厚德宏恩");
            user.setPhone("17732141036");
            user.setStore("九牧王");
            user.setOpenid("oqjs352TaoqrRJ4FEVuSFbXkvj8A");
            user.setAppOpenid("oFteI5NgOCsU19h1-nrfdsE-oWlA");
            user.setStore("远洋播");
            user.setGender("0");

            user.setBirthday("19930918");
            user.setName("刘思远");
            user.setEmp("064");
            g.getData(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
